package com.baic.ibase.config;


import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;

@Configuration
public class MqttConfiguration {

    @Autowired
    private MqttPropertiesConfig mqttPropertiesConfig;

    @Bean
    public MqttPahoClientFactory mqttPhoClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true);
        options.setUserName(mqttPropertiesConfig.getUsername());
        options.setPassword(mqttPropertiesConfig.getPassword().toCharArray());
        options.setServerURIs(new String[]{mqttPropertiesConfig.getUrl()});
        factory.setConnectionOptions(options);
        return factory;
    }

}
